package hello;

/**
 * Created with IntelliJIDEA.
 * Description:
 * User: ASUS
 * Date: 2025/9/11
 * Time: 22:21
 */
public class StringReplaceDemo {
    public static void main(String[] args) {
        String text = "hello java, hello world, I love java!";

        // 1. replace(CharSequence, CharSequence): 替换所有匹配的字符序列
        // 将所有的 "java" 替换为 "Python"
        String replacedText1 = text.replace("java", "Python");
        System.out.println("replace 示例: " + replacedText1);

        // 2. replaceAll(String regex, String replacement): 使用正则表达式替换
        String data = "user123, product456, order789";
        // 将所有数字 (正则表达式 \\d+) 替换为 "ID"
        String replacedText2 = data.replaceAll("\\d+", "ID");
        System.out.println("replaceAll 示例: " + replacedText2);

        // 3. replaceFirst(String regex, String replacement): 只替换第一个匹配的
        // 只将第一个 "hello" 替换为 "hi"
        String replacedText3 = text.replaceFirst("hello", "hi");
        System.out.println("replaceFirst 示例: " + replacedText3);
    }
}
